The "opposite" of <=
is >
.
Often there are several options that might be included in a major purchase. Each one might be accepted or rejected. Pretend you are buying a new car:
You have decided to buy a new sports car. The base price is $20,000. There are two options:
- pin strips — $250
- anti-lock brakes — $800
The price of the car will be the base price plus the price of the options you have picked. Write a program that calculates the price of the car.
Here is an incomplete version. The user is expected to enter "1" to mean true and "0" to mean false. This is not a good way to do this, but the better methods have not yet been covered in these notes.
import java.io.*; class CarPurchase { public static void main (String[] args) throws IOException { final int basePrice = 2000000; // base price in cents final int pinPrice = 25000; // pin stripe price final int brakePrice = 80000; // anti-lock brake price BufferedReader stdin = new BufferedReader ( new InputStreamReader( System.in ) ); String inData; int answer; int totalCost = basePrice; System.out.println("Do you want pin stripes (0 or 1)?"); inData = stdin.readLine(); answer = Integer.parseInt( inData ); if ( __________________ ) { totalCost = totalCost + pinPrice; } System.out.println("Do you want anti-lock brakes (0 or 1)?"); inData = stdin.readLine(); answer = Integer.parseInt( inData ); if ( __________________ ) { totalCost = totalCost + brakePrice; } System.out.println("Total cost is: $" + (totalCost/100) + "." + totalCost%100 ); } }
Notice how the number in totalCost is accumulated: it is initialized in its declaration, then added to in each of the true branches.